<template>
  <div class="component-score-detail">
    <el-table
      :data="dataList"
      header-cell-class-name="table-header"
      row-class-name="table-row-low"
      class="user-edit-table"
      style="width: 100%"
    >
      <el-table-column
        prop="createTime"
        :label="$t('user.changeTime')"
      />
      <el-table-column
        prop="source"
        :label="$t('user.changeReason')"
      >
        <template #default="scope">
          <span v-if="scope.row.source===4 && scope.row.ioType === 1">{{ $t('user.refundRetrunScore') }}</span>
          <span v-else>{{ sourceList[scope.row.source] }}</span>
          <span v-if="scope.row.isLock">{{ $t('user.lockScore') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        label="详情"
        show-overflow-tooltip
      >
        <template #default="scope">
          {{ scope.row.remark || '--' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="score"
        :label="$t('user.changeScoreNum')"
      />
      <el-table-column
        prop="ioType"
        :label="$t('shop.ioType')"
      >
        <template #default="scope">
          <span>{{ [$t('shop.pay'),$t('shop.income')][scope.row.ioType] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-if="dataList.length"
      :current-page="page.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.pageSize"
      :total="page.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="onSizeChangeHandle"
      @current-change="onCurrentChangeHandle"
    />
    <!-- /分页 -->
  </div>
</template>

<script setup>
const sourceList = [
  $t('user.registerScore'),
  $t('user.shopping'),
  $t('user.levelUp'),
  $t('user.signIn'),
  $t('user.shoppingDeducteScore'),
  $t('user.scoreExpired'),
  $t('user.rechargeBalance'),
  $t('user.sysChangeScore'),
  '--',
  '登录',
  '邀请注册',
  '反馈被采纳',
  '提交有效建议',
  '绑定手机号',
  '绑定真实姓名',
  '绑定公司信息',
  '绑定职位信息',
  '成为专业会员', // 17
  '下载图纸',
  ' 使用个人空间'
]

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataList = ref([])
let userId = null
onMounted(() => {
  getData()
})

const init = (id) => {
  userId = id
  Object.assign(page, {
    total: 0, // 总页数
    currentPage: 1, // 当前页数
    pageSize: 10 // 每页显示多少条
  })
  getData(page)
}
defineExpose({ init })
// 获取数据
const getData = (pageParam) => {
  if (!userId) {
    return
  }
  http({
    url: http.adornUrl('/user/userScoreLog/pageByUserId'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        { userName: userId }
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
  })
}

// 每页数
const onSizeChangeHandle = (val) => {
  page.pageSize = val
  page.currentPage = 1
  getData(page)
}
// 当前页
const onCurrentChangeHandle = (val) => {
  page.currentPage = val
  getData(page)
}

</script>

<style lang="scss" scoped>
.component-score-detail{
  .table-template > * {
    display: inline-block;
    margin: auto;
  }
}
</style>
